CC = gcc
CFLAGS = -g -O -Wall
CFLAGS += -fprofile-arcs -ftest-coverage

TOP_PATH  = $(shell pwd)
HEAD_PATH = $(TOP_PATH)/include
SRC_PATH  = $(TOP_PATH)/src
OUT_PATH  = $(TOP_PATH)/bin
TEST_PATH = $(TOP_PATH)/test
DEMO_PATH = $(TOP_PATH)/demo

TARGET_TEST =  $(OUT_PATH)/AllTest
TARGET_MAIN =  $(OUT_PATH)/main
OBJS        =  $(OUT_PATH)/log.o $(OUT_PATH)/keccak256.o $(OUT_PATH)/ecc.o $(OUT_PATH)/hash.o $(OUT_PATH)/account.o $(OUT_PATH)/largefile.o $(OUT_PATH)/x509.o $(OUT_PATH)/json.o $(OUT_PATH)/md5.o
OBJS_TEST   =  $(OBJS) $(OUT_PATH)/all_test.o  $(OUT_PATH)/CuTest.o $(OUT_PATH)/x509_test.o $(OUT_PATH)/account_test.o $(OUT_PATH)/keccak256_test.o $(OUT_PATH)/ecc_test.o
OBJS_MAIN   =  $(OBJS) $(OUT_PATH)/main.o

CFLAGS += -I$(HEAD_PATH)/types/
CFLAGS += -I$(HEAD_PATH)/base/
CFLAGS += -I$(HEAD_PATH)/proto/

$(TARGET_MAIN):	$(OBJS_MAIN)
	$(CC) $(CFLAGS) $^ -o $@

$(TARGET_TEST):	$(OBJS_TEST)
	$(CC) $(CFLAGS) $^ -o $@

# ------- demo -------
$(OUT_PATH)/main.o: $(DEMO_PATH)/main.c
	$(CC) $(CFLAGS) -c $^ -o $@

# ------- test-------
$(OUT_PATH)/all_test.o: $(TEST_PATH)/all_test.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/CuTest.o: $(TEST_PATH)/CuTest.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/x509_test.o: $(TEST_PATH)/x509_test.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/account_test.o: $(TEST_PATH)/account_test.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/keccak256_test.o: $(TEST_PATH)/keccak256_test.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/ecc_test.o: $(TEST_PATH)/ecc_test.c
	$(CC) $(CFLAGS) -c $^ -o $@

# ------- base -------
$(OUT_PATH)/log.o: $(SRC_PATH)/base/log.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/keccak256.o: $(SRC_PATH)/base/keccak256.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/ecc.o: $(SRC_PATH)/base/ecc.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/hash.o: $(SRC_PATH)/base/hash.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/x509.o: $(SRC_PATH)/base/x509.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/json.o: $(SRC_PATH)/base/json.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/md5.o: $(SRC_PATH)/base/md5.c
	$(CC) $(CFLAGS) -c $^ -o $@
# ------- proto -------
$(OUT_PATH)/account.o: $(SRC_PATH)/proto/account.c
	$(CC) $(CFLAGS) -c $^ -o $@

$(OUT_PATH)/largefile.o: $(SRC_PATH)/proto/largefile.c
	$(CC) $(CFLAGS) -c $^ -o $@

all: prepare $(TARGET_MAIN) $(TARGET_TEST)


prepare:
	mkdir -p $(OUT_PATH)

clean:
	rm -rf $(OUT_PATH)
